package com.topway.dsmflex.services.synchro;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import com.Consts;
import com.topway.dsmflex.dao.synchro.TableDao;
import com.topway.dsmflex.model.synchro.DataColumnBean;
import com.topway.dsmflex.model.synchro.DataFileBean;
import com.topway.dsmflex.model.synchro.DataSourceBean;
import com.topway.dsmflex.services.synchro.base.AbstractBaseServices;
import com.topway.dsmflex.util.ConvertUtil;



/**
 * 
 * @author liusy 2013-05-8
 *
 */
public class FileServices extends AbstractBaseServices<DataFileBean>{

	public FileServices(String dataFilePath) {
		super(dataFilePath);
		this.tableDao = TableDao.getTableDao();
		// TODO Auto-generated constructor stub
	}
	
	private static FileServices fileServices = new FileServices(Consts.DATAFILE_FILE);
	protected TableDao tableDao = null;

	
	public synchronized static FileServices getFileServices()
	{
		return fileServices;
	}
	public List<DataColumnBean> getColumnInfo(DataFileBean dt)
	{
	 	List<DataColumnBean> dcListT = new ArrayList<DataColumnBean>();
	 	try {
	 	dcListT = (List<DataColumnBean>) tableDao.getColumnInfo(dt.getTargetDataSource(), dt.getTableNameTarget());
	 	this.setSourceColumn(tableDao.getSourceColumn());
	 	if(mapTable.containsKey( dt.getTableNameSource()))
	 	{
	 		DataFileBean _dt = mapTable.get(dt.getName());
	 		List<DataColumnBean> dcListS = _dt.getDataColumnBean();
	 		if(null==dcListS || dcListS.size()==0)
	 			return dcListT;
	 		DataColumnBean dcT;
	 		DataColumnBean dcS;
			for (int j = 0; j < dcListT.size(); j++) {
				dcT = dcListT.get(j);
		 		for (int i = 0; i < dcListS.size(); i++) {
					dcS = dcListS.get(i);
					if(dcT.getColumnNameSource().equals(dcS.getColumnNameSource()))
						try {
							ConvertUtil.convertToPool(dcT, dcS);
						} catch (Exception e) {
							e.printStackTrace();
						}
				}
			}
	 	}
		} catch (Exception e) {
			e.printStackTrace();
		}
	 	return dcListT;
    }
	
	public List<DataFileBean> getAllDataTable(DataSourceBean  dsSource) {
		List<DataFileBean> list= new ArrayList<DataFileBean>();
    	for (Entry<String, DataFileBean> entry : mapTable.entrySet()) {
    		list.add(entry.getValue());
    	}	
    	return list;
	}
	
	public String [] getTargetTablesByDb(DataFileBean df)
	{
		return this.tableDao.getAllDataTableArray(df.getTargetDataSource());
	}
	
	public String [] getColumnInfoArray(DataFileBean dt)
	{
		return  this.getSourceColumn();
    }
	
}
